package com.yiren.dbaa.module.project_edit.service.impl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.text.InputType;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.yiren.dbaa.R;
import com.yiren.dbaa.module.project_edit.service.ICreateView;
import com.yiren.dbaa.view.FlowerLayout;


/**
 * @author lwb
 * @date : 2022/4/8 9:15
 * @description: 动态创建控件实现
 */
public class CreateViewImpl implements ICreateView {

    @Override
    public TextView addTextView(Context context) {
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, (int) context.getResources().getDimension(R.dimen.dp_30));
        lp.setMargins((int) context.getResources().getDimension(R.dimen.dp_10), 0, 0, 0);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setTextAppearance(context, R.style.param_set_tv_rule_style);
        return textView;
    }

    @Override
    public TextView addVoiceValueTextView(Context context, int flag) {
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(flag == 0 ? 0 : (int) context.getResources().getDimension(R.dimen.dp_10), 0, 0, 0);
        textView.setLayoutParams(lp);
        textView.setTextAppearance(context, R.style.param_set_title_style);
        return textView;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public EditText addEditText(Context context) {
        EditText editText = new EditText(context);
        editText.setPadding((int) context.getResources().getDimension(R.dimen.dp_20), 0, (int) context.getResources().getDimension(R.dimen.dp_20), 0);
        editText.setBackgroundResource(R.drawable.border_black_selector);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        editText.setTextAppearance(context, R.style.param_set_et_content_style);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, (int) context.getResources().getDimension(R.dimen.dp_30));
        lp.setMargins((int) context.getResources().getDimension(R.dimen.dp_10), 0, 0, 0);
        editText.setLayoutParams(lp);
        return editText;
    }

    @Override
    public EditText addVoiceValueEditText(Context context) {
        EditText editText = new EditText(context);
        editText.setPadding((int) context.getResources().getDimension(R.dimen.dp_20), 0, (int) context.getResources().getDimension(R.dimen.dp_20), 0);
        editText.setBackgroundResource(R.drawable.border_black_selector);
        editText.setTextAppearance(context, R.style.param_set_et_content_style);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, (int) context.getResources().getDimension(R.dimen.dp_30));
        lp.setMargins((int) context.getResources().getDimension(R.dimen.dp_10), 0, 0, 0);
        editText.setLayoutParams(lp);
        return editText;
    }

    @Override
    public CheckBox addCheckBox(Context context) {
        CheckBox checkBox = new CheckBox(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        checkBox.setLayoutParams(lp);
        checkBox.setGravity(Gravity.CENTER_VERTICAL);
        checkBox.setButtonDrawable(R.drawable.checkbox_common_selector);
        return checkBox;
    }

    @Override
    public CheckBox addChildCheckBox(Context context) {
        CheckBox checkBox = new CheckBox(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins((int) context.getResources().getDimension(R.dimen.dp_30), (int) context.getResources().getDimension(R.dimen.dp_5), 0, 0);
        checkBox.setLayoutParams(lp);
        checkBox.setButtonDrawable(R.drawable.radiobutton_public_selector);
        return checkBox;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public FlowerLayout addParentFlowerLayout(Context context) {
        FlowerLayout flowerLayout = new FlowerLayout(context, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
        flowerLayout.setLayoutParams(lp);
        return flowerLayout;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public FlowerLayout addChildFlowerLayout(Context context) {
        FlowerLayout flowerLayout = new FlowerLayout(context, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        flowerLayout.setLayoutParams(lp);
        return flowerLayout;
    }

    @Override
    public LinearLayout addChildLinearLayout(Context context) {
        LinearLayout linearLayout = new LinearLayout(context);
//        linearLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, (int) context.getResources().getDimension(R.dimen.dp_8), 0, (int) context.getResources().getDimension(R.dimen.dp_8));
        linearLayout.setLayoutParams(lp);
        return linearLayout;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public Spinner addSpinner(Context context) {
        Spinner spinner = new Spinner(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) context.getResources().getDimension(R.dimen.dp_60), (int) context.getResources().getDimension(R.dimen.dp_30));
        lp.setMargins((int) context.getResources().getDimension(R.dimen.dp_10), 0, 0, 0);
        spinner.setLayoutParams(lp);// Width , height
        spinner.setBackgroundResource(R.drawable.spinner_bg_shape);
        spinner.setGravity(Gravity.CENTER_VERTICAL);
        return spinner;
    }

    @Override
    public RadioGroup addRadioGroup(Context context) {
        RadioGroup radioGroup = new RadioGroup(context);
        return radioGroup;
    }

    @Override
    public RadioButton addRadioButton(Context context) {
        RadioButton radioButton = new RadioButton(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) context.getResources().getDimension(R.dimen.dp_20), (int) context.getResources().getDimension(R.dimen.dp_20));
        radioButton.setLayoutParams(lp);
        radioButton.setButtonDrawable(R.drawable.radiobutton_public_selector);
        return radioButton;
    }

    @Override
    public TextView addDividerLine(Context context) {
        TextView tv = new TextView(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.dp_1));
        tv.setLayoutParams(lp);
        tv.setBackgroundColor(context.getResources().getColor(R.color.dbaf_division_line_gray));
        return tv;
    }

}
